'''Map handling module

Defines the classes for maps and its walls, as well as provides functions for
working with those. Provided functionalities are:
- Loading and saving maps.
- Drawing maps.'''

from os.path import join

from pygame import draw, image, rect
from random import randint

from text_file         import *
from locals            import *
from extended_graphics import *

class Wall:
    def __init__(self, points):
        '''The points that form the wall should be given in a clockwise
        direction otherwise collision detection may be faulty.
        This type of wall has no appearance.'''
        self.points       = points
        self.lines        = []
        for x in xrange(0, len(self.points)):
            self.lines.append((points[x], points[(x+1)%len(self.points)]))
    
    def draw(self, surface):
        pass

class SolidColorWall(Wall):
    def __init__(self, points, color):
        '''This type of wall is filled with a solid color.'''
        Wall.__init__(self, points)
        self.color        = (color[0]/2, color[1]/2, color[2]/2)
        self.bordercolor  = color
    
    def draw(self, surface):
        draw.polygon(surface, self.color, self.points)
        draw.polygon(surface, self.bordercolor, self.points, 1)

class Map:
    def __init__(self, file_name):
        self.name          = None
        self.author        = None
        self.author_email  = None
        self.background    = None
        self.description   = None
        self.walls         = []
        self.external_wall = Wall([(0,0), (0, 600), (800, 600), (800, 0)])
        self.load_map(file_name)

    def load_map(self, file_name):
        text_file = TextFile(file_name)

        #################################
        # Grab basic header information #
        #################################
        self.name         = text_file.seek_attribute("name")
        self.author       = text_file.seek_attribute("author")
        self.author_email = text_file.seek_attribute("authoremail")
        self.description  = text_file.seek_attribute("description")

        ############################
        # Grab invisible wall data #
        ############################
        walls_data = text_file.seek_attribute_list("invisible_wall")
        print walls_data
        for wall in walls_data:
            wall_data = wall.split()
            points_data = [int(p) for p in wall_data]
            points = []
            for x in xrange(0, len(points_data), 2):
                points.append((points_data[x], points_data[x+1]))            
            self.walls.append(Wall(points))

        ##########################
        # Grab visible wall data #
        ##########################
        walls_data = text_file.seek_attribute_list("wall")
        for wall in walls_data:
            wall_data = wall.split()
            color     = (int(wall_data[0]), int(wall_data[1]), int(wall_data[2]))

            points_data = [int(p) for p in wall_data[3:]]
            points = []
            for x in xrange(0, len(points_data), 2):
                points.append((points_data[x], points_data[x+1]))
            
            self.walls.append(SolidColorWall(points, color))

        ##############################
        # Grab background image data #
        ##############################
        background_file = text_file.seek_attribute("background")
        if background_file:
            img = image.load(join("data", "backgrounds", background_file))
            self.background = img.convert()
        else:
            self.background = None

        harmed_background_file = text_file.seek_attribute("harmed_background")
        if harmed_background_file:
            img = image.load(join("data", "backgrounds", harmed_background_file))
            self.harmed_background = img.convert()
        else:
            self.harmed_background = None

    def burn(self, pos, alpha):
        if self.harmed_background == None:
            return
        global b_window
        r1 = rect.Rect(0, 0, 64, 64)
        r1.center = pos
        r2 = rect.Rect(randint(0, 2)*64, randint(0, 2)*64, 64, 64)
        blit_through_window(self.harmed_background, r1, self.background, r1, b_window, r2, alpha)
        return r1
    
    def draw(self, surface):
        surface.blit(self.background, (0, 0))
#        draw.rect(surface, WHITE, ((0, 0), (map_x_limit, map_y_limit)), 1)
        for wall in self.walls:
            wall.draw(surface)

def map_def_init():
    global b_window
    b_window = load_blit_window(join("data", "images", "explosion_holes.png"))

